package api

import (
	"github.com/kataras/iris/v12"
	"main/store"
	"main/turkey"
	"strconv"
)

type SensorHandler struct {
	service store.ISensorService
}

func NewSensorHandler(service store.ISensorService) *SensorHandler {
	return &SensorHandler{service: service}
}

func (s *SensorHandler) GetData(ctx iris.Context) {
	iedno := ctx.URLParam("iedno")
	i, err := strconv.Atoi(iedno)
	if err != nil {
		ctx.JSON(Res(turkey.NotOK, turkey.D___(err.Error()), iris.Map{}))
		return
	}
	res, err := s.service.GetData(i)
	if err != nil {
		ctx.JSON(Res(turkey.NotOK, turkey.D___(err.Error()), iris.Map{}))
		return
	}
	ctx.JSON(Res(turkey.OK, turkey.D___("OK"), res))
	return
}
